home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_006 / setserial / setserial.c < prev   
C/C++ Source or Header  |  1992-05-06  |  7KB  |  205 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* SetSerial allows the CLI user to dynamically change any particular
  31.  * serial port parameter.
  32.  *
  33.  * July 6, 1985  Version 1.0  Keith Stobie  Initial version 
  34.  * July 7, 1985  Version 2.0  Keith Stobie  Converted to Lattice
  35.  * July 8, 1985  Version 3.0  Keith Stobie  Both term words settable.
  36.  * Sep 25, 1985  Version 3.1  Tom Pohorsky  Remove WBufLen
  37.  * Nov  9, 1985  Version 3.2  Tom Pohorsky  Cleanup
  38.  */
  39. char *prog_name;            /* Name of program being run */
  40. char *prog_version = "3.2"; /* Version of the program */
  41.  
  42. #include        <exec/types.h>
  43. #include        <exec/nodes.h>
  44. #include        <exec/lists.h>
  45. #include        <exec/memory.h>
  46. #include        <exec/ports.h>
  47. #include        <exec/libraries.h>
  48. #include        <exec/devices.h>
  49. #include        <exec/tasks.h>
  50. #include        <exec/io.h>
  51. #include        <devices/serial.h>
  52. #undef NULL
  53. #include        <lattice/stdio.h>
  54.  
  55.  
  56. extern struct MsgPort *CreatePort();
  57.  
  58. int set_serial( index, value )
  59.    int   index;         /* which parameter to set */
  60.    ULONG value;      /* The new value for paramter */
  61. {
  62.     struct IOExtSer IORser;      /* Serial port IO request block */
  63.     int     error;
  64.  
  65.     if ((index < 0) || (index > 11)) {
  66.        printf( "Index value %ld is not in range 0-10!\n", index );    
  67.        return 20;
  68.     }
  69.  
  70.     IORser.io_SerFlags |= SERF_SHARED;
  71.     if ((error = OpenDevice (SERIALNAME, 0, &IORser, 0)) != 0) {
  72.         printf( "Unable to open Serial Device, error=%ld\n", error );
  73.         return 20;
  74.     }
  75.  
  76.     IORser.IOSer.io_Command = SDCMD_QUERY;
  77.     if ((error = DoIO( &IORser ) != 0)) {
  78.         printf ( "Query status error %ld\n", error);
  79.         return 20;
  80.     }
  81.  
  82.     /* SET UP the read message port in the I/O request */
  83.     if ((IORser.IOSer.io_Message.mn_ReplyPort = CreatePort( "SetSerial", 0 ))
  84.          == NULL) {
  85.         printf( "Unable to create port for IO message\n" );
  86.         CloseDevice( &IORser );
  87.         return 20;
  88.       }
  89.  
  90.     switch( index ) {
  91.       case 0:    print_request( &IORser );         break;
  92.       case 1:    IORser.io_CtlChar       = value;  break;
  93.       case 2:    IORser.io_RBufLen       = value;  break;
  94.       case 3:    IORser.io_Baud          = value;  break;
  95.       case 4:    IORser.io_BrkTime       = value;  break;
  96.       case 5:    IORser.io_TermArray.TermArray0 = value; break;
  97.       case 6:    IORser.io_TermArray.TermArray1 = value; break;
  98.       case 7:    IORser.io_ReadLen       = value;  break;
  99.       case 8:    IORser.io_WriteLen      = value;  break;
  100.       case 9:    IORser.io_StopBits      = value;  break;
  101.       case 10:   IORser.io_SerFlags      = value;  break;
  102.       default:   printf( "Internal Logic error! case value %ld\n", index );
  103.     } /* switch */
  104.  
  105.     IORser.IOSer.io_Command = SDCMD_SETPARAMS;
  106.  
  107.     error = DoIO( &IORser );
  108.  
  109.     DeletePort( IORser.IOSer.io_Message.mn_ReplyPort );
  110.  
  111.     CloseDevice( &IORser );
  112.  
  113.     if (error) {
  114.        printf( "Error %ld doing IO to set params!\n", error );
  115.        return 10;
  116.     }
  117.       
  118.     if (IORser.IOSer.io_Error) {
  119.        printf( "Error %ld from serial device doing set params!\n"
  120.              , IORser.IOSer.io_Error );
  121.        return 10;
  122.     }
  123.  
  124.     return 0;
  125. }  /* set_serial() */
  126.  
  127.  
  128.  
  129.  
  130. print_request( IORser )
  131.     struct IOExtSer *IORser;     /* Serial port IO request block */
  132. {
  133. #define PRINT( field )           printf( "      %s = %8lx %8ld\n" \
  134.                                  , "field", (ULONG) IORser->field \
  135.                                  , (ULONG) IORser->field )
  136. #define IPRINT( index, field ) printf( " %2ld   %s = %8lx %8ld\n" \
  137.                                     , index, "field", (ULONG) IORser->field \
  138.                                                     , (ULONG) IORser->field )
  139.  
  140.       printf( "index field name       hexadec  decimal\n" ); 
  141.       IPRINT( 1, io_CtlChar   );
  142.       IPRINT( 2, io_RBufLen   );
  143.       IPRINT( 3, io_Baud      );
  144.       IPRINT( 4, io_BrkTime   ); 
  145.       IPRINT( 5, io_TermArray.TermArray0   );
  146.       IPRINT( 6, io_TermArray.TermArray1   );
  147.       IPRINT( 7, io_ReadLen   );
  148.       IPRINT( 8, io_WriteLen  );
  149.       IPRINT( 9, io_StopBits  );
  150.       IPRINT(10, io_SerFlags  );
  151.       printf( "\n" );   /* Not associated with an index */
  152.           PRINT( io_Status    );
  153. }  /* print_request() */
  154.  
  155.  
  156. print_usage() {
  157.    printf("%s: version %s\n", prog_name, prog_version );
  158.    printf("usage: %s <index> <value>\n", prog_name );
  159.    printf("  <index> is a decimal number indicating which parameter.\n" );
  160.    printf("          0 indicates print current values (and indexes) \n");
  161.    printf("            without changing them.\n" );
  162.    printf("  <value> number to set the indexed parameter to.\n");
  163.    printf("          value should be in decimal unless it starts with X\n" );
  164.    printf("          or x in which case the number should be hexadecimal\n");
  165.    exit( 5 );
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172. main( argc, argv ) 
  173.    int argc;
  174.    char *argv[];
  175. {
  176.    int   index;
  177.    ULONG value;
  178.  
  179.    if (argc <=0 ) { prog_name = "SetSerial"; }
  180.    else { prog_name = argv[0];}
  181.  
  182.    if (argc == 1) { print_usage(); }
  183.  
  184.    if (argc < 2 ) { printf( "Too few parameters\n" ); print_usage();}
  185.    if (argc > 3 ) { printf( "Too many parameters\n" ); print_usage();}
  186.  
  187.    sscanf( *++argv, "%d", &index );
  188.  
  189.    if ((index != 0) && (argc < 3)) {
  190.       printf( "Too few parameters\n" ); print_usage();
  191.    }
  192.  
  193.    if (argc == 3) { 
  194.       ++argv;
  195.       if ((*argv[0] == 'x') || (*argv[0] == 'X')) {
  196.          sscanf( *argv+1, "%x", &value ); /* Skip x or X */
  197.       } else {
  198.          sscanf( *argv, "%d", &value );
  199.       }
  200.    }
  201.  
  202.    exit( set_serial( index, value ) ); 
  203.    
  204. }  /* main() */
  205.